package com.study01;

import java.io.*;
import java.util.Arrays;

public class Demo02 {
    public static void main(String[] args) {
//        copyFiles("D:/jack/work/js-before", "E:/desk");
        test01();
    }

    public static void copyFiles(String srcPath, String destPath) {
        File srcFile = new File(srcPath);
        File destFile = new File(destPath);
        if (srcFile.isFile()) {
            try (
                InputStream is = new FileInputStream(srcFile);
                OutputStream os = new FileOutputStream(destPath + File.separator + srcFile.getName());
            ){
                byte[] buffer = new byte[1024 * 1024];
                int num;
                while ((num = is.read(buffer)) != -1) {
                    os.write(buffer, 0, num);
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            destFile = new File(destPath + File.separator + srcFile.getName());
            destFile.mkdirs();
            File[] files = srcFile.listFiles();
            for(File file : files){
                copyFiles(file.getAbsolutePath(),destFile.getAbsolutePath());
            }
        }
    }
    public static void test01(){
        File file = new File("d:/a.txt");
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(file);
            byte[] buffer = new byte[5];
            int num;
            while((num = fis.read(buffer)) != -1){
                System.out.println(new String(buffer, 2, 4));  // A B C D E
                System.out.println(num);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(fis != null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    public static void test02(){
        try {
            FileOutputStream fis = new FileOutputStream("user.data");
            ObjectOutputStream oos = new ObjectOutputStream(fis);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
